package com.jy.adscm.operation.lxcg.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.lxcg.bean.LxcgTaskDetail;
import com.jy.adscm.operation.lxcg.service.LxcgTaskDetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购详情 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@RestController
@RequestMapping("/adscm/operation/lxcg/lxcgTaskDetail")
public class LxcgTaskDetailController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    LxcgTaskDetailService lxcgTaskDetailService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody LxcgTaskDetail lxcgTaskDetail, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        lxcgTaskDetail.setCreator(token.getUser().getId().toString());
        lxcgTaskDetail.setCreateTime(new Date());
        if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getPrice(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(lxcgTaskDetail.getNums(),null)){
                lxcgTaskDetail.setTotal(lxcgTaskDetail.getPrice()*lxcgTaskDetail.getNums());
            }
        }
        boolean insert = lxcgTaskDetailService.insert(lxcgTaskDetail);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody LxcgTaskDetail lxcgTaskDetail, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        boolean result = lxcgTaskDetailService.updateLxcgTaskDetail(lxcgTaskDetail,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("delete/{id}")
    public  SimpleCoreResponse  delete(@PathVariable Long id, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        boolean result = lxcgTaskDetailService.deleteById(id);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    /**
     * 根据图纸tid采购详情信息
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectByTid/{tid}")
    public  SimpleCoreResponse  selectByTid(@PathVariable Long tid , HttpServletRequest request) throws  AdscmSystemException{
        List<LxcgTaskDetail> cgTaskDetails = lxcgTaskDetailService.selectByTid(tid);
        return  new SimpleCoreResponse("200","查询成功",cgTaskDetails);
    }

}

